<?php

class AttachUtil {
	
	public static function upload(&$attach, $nameInputFile,$codiceAttachTipo,$pathDinamico=null) {
    	try {
       		if (isset($_FILES[$nameInputFile]["name"]) && !empty($_FILES[$nameInputFile]["name"])) {
				// $this->Attach->setDataSource($this->Libro->getDataSource());
				$attach->set('file_name', $_FILES[$nameInputFile]["name"]);
				// $this->Attach->set('path', $this->request->data['path']);
				$attach->set('attach_tipo', $codiceAttachTipo);
				$attach->set('dimensione', $_FILES[$nameInputFile]["size"]);
				$attach->set('tipo', $_FILES[$nameInputFile]["type"]);
				$attach->set('path_dinamico',$pathDinamico);
				$success = move_uploaded_file($_FILES[$nameInputFile]["tmp_name"], TMP."uploads".DS.pathinfo($_FILES[$nameInputFile]["tmp_name"],PATHINFO_BASENAME));
				if (!$success) {
					throw new Exception(__("Errore in fase di copia dell'attach sulla cartella tmp."));
				}
				$attach->setFileTemporaneo(TMP."uploads/".pathinfo($_FILES[$nameInputFile]["tmp_name"],PATHINFO_BASENAME));
				$attach->save();
				return true;
    		} else {
    			return false;
    		}
    	} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw $e;
    	}
	}
	
	public static function saveAttachFromFile(&$attach, $file, $codiceAttachTipo,$pathDinamico=null) {
    	try {
       		if (!empty($file) && !empty($codiceAttachTipo)) {
				$attach->set('file_name', pathinfo($file,PATHINFO_BASENAME));
				$attach->set('attach_tipo', $codiceAttachTipo);
				$attach->set('dimensione', filesize($file));
				$attach->set('tipo', pathinfo($file,PATHINFO_EXTENSION));
				$attach->set('path_dinamico',$pathDinamico);
				$attach->setFileTemporaneo($file);
				$attach->save();
				return true;
    		} else {
    			return false;
    		}
    	} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw $e;
    	}
	}
	

	// sposta un dato attach in un altro tipo
	public static function moveAttach($attach,$attachTipo) {
		try {
			$attachSource = null;
			$attachDest = null;
			if ($attach instanceof Attach) {
				$attachSource = $attach;
			} else {
				$attachSource = new Attach();
				$attachSource->read(null,$attach);
			}
			$attachDest=$this->copyAttach($attachSource,$attachTipo);
			$attachSource->delete();
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw $e;
		}
	}

	// copia attach
	public static function copyAttach($attach, $attachTipo) {
		try {
			$attachSource = null;
			$attachDest = null;
			if ($attach instanceof Attach) {
				$attachSource = $attach;
			} else {
				$attachSource = new Attach();
				$attachSource->read(null,$attach);
			}
			// verifico che l'attach dato non sia dello stesso tipo
			if ($attachSource->field("attach_tipo")==$attachTipo) {
				throw new Exception(__("Impossibile spostare un attach dello stesso tipo."));
			}
			$attachDest = new Attach();
			$this->copyAttachModel($attachSource, $attachDest);
			$attachDest->set("attach_tipo",$attachTipo);
			$attachDest->setFileTemporaneo($attachSource->getFile(),false);
			$attachDest->save();
			return $attachDest;
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw $e;
		}
	}

	private function copyAttachModel($src,$dest) {
		$dest->set("file_name",$src->field("file_name"));
		$dest->set("path",$src->field("path"));
		$dest->set("dimensione",$src->field("dimensione"));
		$dest->set("tipo",$src->field("tipo"));
		$dest->set("descrizione",$src->field("descrizione"));
		$dest->set("ordine",$src->field("ordine"));
		$dest->set("visibile",$src->field("visibile"));
	}
	

}

?>